import jpype
import jaydebeapi

def connect_to_oracle():
    # 修改后的 JDBC 配置
    jdbc_url = 'jdbc:oracle:thin:@//10.1.161.53:11521/helowin'  # 修改IP、端口和服务名
    driver = 'oracle.jdbc.driver.OracleDriver'
    jar_path = r'D:\Py\llm_workflow\Database\ojdbc6-11.2.0.3.jar'  # 确保路径正确
    user = 'sys'           # 保持用户名不变
    password = 'oracle'    # 修改密码

    # jdbc_url = 'jdbc:oracle:thin:@//10.1.161.116:1521/ORCL'  # 修改IP、端口和服务名
    # driver = 'oracle.jdbc.driver.OracleDriver'
    # jar_path = r'D:\Py\llm_workflow\Database\ojdbc6-11.2.0.3.jar'  # 确保路径正确
    # user = 'sys'           # 保持用户名不变
    # password = '123456'    # 修改密码

    # 启动 JVM（仅首次调用时执行）
    if not jpype.isJVMStarted():
        jpype.startJVM(
            jpype.getDefaultJVMPath(),
            "-Djava.class.path={}".format(jar_path)
        )

    try:
        # 连接参数（含 SYSDBA 权限）
        driver_args = {
            'user': user,
            'password': password,
            'internal_logon': 'sysdba'  # 保持SYSDBA权限
        }

        # 建立连接
        conn = jaydebeapi.connect(
            driver,
            jdbc_url,
            driver_args,
            jar_path
        )
        print("数据库连接成功！")
        return conn  # 返回连接对象

    except Exception as e:
        print(f"数据库连接失败: {str(e)}")
        return None  # 连接失败返回空